package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhs.storage.bean.PersonnelFiles;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.dao.PersonnelFilesDao;
import com.hhs.storage.service.PersonnelFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chx
 * @since 2024-01-25
 */
@Service
public class PersonnelFilesServiceImpl extends ServiceImpl<PersonnelFilesDao, PersonnelFiles> implements PersonnelFilesService {

    @Value("${upload.folderName}")
    private String folderName;

    @Resource
    private HttpServletRequest request;

    @Override
    public Map<String, Object> uploadFiles(PersonnelFiles personnelFiles, MultipartFile[] files) {

        if (files == null || files.length < 0) {
            return ResponseInfo.error("文件为空");
        }

        LocalDateTime localDateTime = LocalDateTime.now();
        String prevent = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        if (personnelFiles.getUploadType() == null) {
            return ResponseInfo.error("请选择上传类型");
        }

        String folder = "";

        if (personnelFiles.getUploadType() == 1) {
            folder = "/" + personnelFiles.getFolderName();
            File file = new File(folderName + folder);
            if (!file.exists()) {
                file.mkdir();
            }
        }

        personnelFiles.setUserId(SysUtil.getLoginUserId(request));

        List<PersonnelFiles> list=new ArrayList<>();

        for (MultipartFile file : files) {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                File file1 = new File(folderName + folder, prevent + file.getOriginalFilename());
                file.transferTo(file1);
                personnelFiles.setFileUrl(folder + "/" + prevent + file.getOriginalFilename());
                personnelFiles.setId(SysUtil.UUID());
                personnelFiles.setCreateTime(LocalDateTime.now());
                list.add(personnelFiles);
            } catch (IOException e) {
                return ResponseInfo.error("文件上传失败");
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        this.saveBatch(list);

        return ResponseInfo.success("文件上传成功");
    }

}